import six
import settings
import hander
import urllib.request
from bs4 import BeautifulSoup


class Spyder():
	def __init__(self,url,coding='utf-8'):
		self.useragent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
		self.header={"User-Agent":self.useragent}
		self.coding=coding
		self.url=url

	def readmainPage(self):#读取目录页
		html=urllib.request.urlopen(self.url)
		text=html.read().decode(self.coding)
		#print(text)
		return text

	def readtext(self,url,coding='default'):#读取章节页面
		if coding=='default':
			coding=self.coding

		html=urllib.request.urlopen(url)
		page=html.read().decode(self.coding)
		soup = BeautifulSoup(page, "html.parser")
		#text=''
		for content in soup.find_all('div',class_='content'):
				text=content.get_text()
		#print(text)
		return hander.textHander(text)

#if six.PY3:
		#Python3代码
		#import urllib.request as reuqest

		#spyder=Spyder()
		#for i in settings.booklist:
		#	url=settings.urllist[i]
		#	text=spyder.readtext(url)
		#	print(text)
url='http://www.biquge0.com/zread/7/7066'
#html=urllib.request.urlopen(url).read().decode('gbk')

from bs4 import BeautifulSoup
#soup = BeautifulSoup(settings.html_test2, "html.parser")
spyder=Spyder(url,'gbk')
html=spyder.readmainPage()
soup = BeautifulSoup(html, "html.parser")

links=soup.find_all("dd")
print()

#pre_soup=soup.prettify()
#for link in soup.find_all('a'):
#	print(link)
#	print(link.get('href'))
#	print(link.get('id'))
#	print(link.get_text())
print(soup.dd)
if links[1].get('id'):
	print(1)
else:
	print(2)
new_url=url+'/'+links[1].a.get('href')
content=spyder.readtext(new_url)
#print(links[1].a.get('href'))
print(new_url)
print(content.replace(u'\xa0', u' '))
f=open('test.txt','w')
f.write(content.replace(u'\xa0', u' '))
f.close()

print('ok')
